package ocr;

/**
 * @截取图片
 * @date 2022/4/15
 * @time 0:09
 */

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Image {

//    public static void main(String[] args) {
//        try {
//            //注意image要比mask小
//            BufferedImage image = ImageIO.read(Image.class.getResource("C:\\Users\\啊林\\Desktop\\图片检测\\QQ图片20220414212621.jpg"));
//            BufferedImage mask = ImageIO.read(Image.class.getResource("C:\\Users\\啊林\\Desktop\\图片检测\\15_10_48.bmp"));
//
//            File file = new File("C:\\Users\\啊林\\Desktop\\图片检测\\QQ图片20220411233818.jpg");
//
//            int offsetX = mask.getWidth();
//            int offsetY = mask.getHeight();
//            BufferedImage mixedImage = maskImage(image, mask, 100, 100);
//            ImageIO.write(mixedImage, "png", file);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    public static void main(String[] args) {
        //原图片
        String soursePicPath = "C:\\Users\\啊林\\Desktop\\图片检测\\20220423\\14时21分19秒.bmp";
        File sourcePic = new File(soursePicPath);
        try {
            BufferedImage pic1 = ImageIO.read(sourcePic);
            int width = pic1.getWidth();
            int height = pic1.getHeight();
            //参数依次为，截取起点的x坐标，y坐标，截取宽度，截取高度
            BufferedImage pic2 = pic1.getSubimage(129, 322, 100, 100);

            //将截取的子图另行存储
            File desImage = new File("C:\\Users\\啊林\\Desktop\\图片检测\\20220423\\5.bmp");
            ImageIO.write(pic2, "bmp", desImage);
        } catch (IOException e1) {
            e1.printStackTrace();
        }

    }

    public static BufferedImage maskImage(BufferedImage image, BufferedImage mask, Integer offsetX, Integer offsetY) {

        int width = image.getWidth();
        int height = image.getHeight();

        // 关键在于这里新建的图片类型一定是TYPE_INT_ARGB的
        BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        int index = 0;
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                index++;
                System.out.println("第" + index + "个点");
                // result[i][j] = bufImg.getRGB(i, j); int rgb = bufImg.getRGB(i, j);
                int pixel = image.getRGB(i, j);
                Color color = new Color(pixel);
                int r = color.getRed();
                int g = color.getGreen();
                int b = color.getBlue();
                System.out.println("横坐标为：" + i + "；纵坐标为：" + j + "的像素为值为" + pixel);
                System.out.println("\t红=" + r + "\t绿" + g + "\t蓝" + b);
                int alpha = (pixel >> 24) & 0xff;
                color = new Color(alpha);
                r = color.getRed();
                g = color.getGreen();
                b = color.getBlue();
                System.out.println("横坐标为：" + i + "；纵坐标为：" + j + "的像素为值为" + alpha);
                System.out.println("\t红=" + r + "\t绿" + g + "\t蓝" + b);
                int maskPixel = mask.getRGB(offsetX + i, offsetY + j);
                color = new Color(maskPixel);
                r = color.getRed();
                g = color.getGreen();
                b = color.getBlue();
                int x = offsetX + i;
                int y = offsetY + j;
                System.out.println("横坐标为：" + x + "；纵坐标为：" + y + "的像素为值为" + maskPixel);
                System.out.println("\t红=" + r + "\t绿" + g + "\t蓝" + b);
                int newPixel = alpha == 0 ? pixel : maskPixel;
                color = new Color(newPixel);
                r = color.getRed();
                g = color.getGreen();
                b = color.getBlue();
                System.out.println("横坐标为：" + x + "；纵坐标为：" + y + "的新像素为值为" + newPixel);
                System.out.println("\t红=" + r + "\t绿" + g + "\t蓝" + b);
                System.out.println("################################################################");
                newImage.setRGB(i, j, newPixel);
            }
        }
        return newImage;
    }
}